package com.qding.park.manager.web.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.annotation.log.OperationLogType;
import com.qding.park.common.core.annotation.log.SystemControllerLog;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.facade.statis.service.CarStatisFacade;
import com.qding.park.manager.pojo.PowerInfo;
import com.qding.park.manager.pojo.example.TbCarExample;
import com.qding.park.manager.pojo.example.TbCarExample.Criteria;
import com.qding.park.manager.web.form.CarListForm;
import com.qding.park.manager.web.power.Power;

/**
 * @ClassName: Web_CarController
 * @Description: 车辆统计
 * @author Comsys-Administrator
 * @date 2016年10月9日 下午6:00:52
 *
 */
@RestController
@RequestMapping("/car")
public class Web_CarController extends BaseController {

	private static final Logger _log = LoggerFactory.getLogger(Web_CarController.class);

	@Autowired
	private CarStatisFacade carStatisFacade;

	/**
	 * 分页查询车辆列表列表
	 * @param carNum        车牌
	 * @param carNumStatus　　　车牌状态
	 * @param expiryStatus   过期状态
	 * @param jumpPage    当前页
	 * @param pageLength   页长度
	 * @param tel     手机号
	 * @param parkId  车场id
	 * @param monthFee  月费
	 * @return
	 */
	@Power(visitPark="#form.getParkId()")
	@SystemControllerLog(description = "车辆列表查询", operation = OperationLogType.car_list)
	@RequestMapping("/list")
	public AnswerMsg list(@Valid CarListForm form,BindingResult result) {
				/*解析验证结果*/
				super.analysisValid(result);
				/*解析验证结果*/
			PowerInfo power = this.getPowerInfo();


			int jumpPageInt = Integer.valueOf(form.getJumpPage());
			int pageLengthInt = Integer.valueOf(form.getPageLength());
			
			TbCarExample  example=new TbCarExample();
			Criteria criteria = example.createCriteria();
			
			if (!StringUtils.isBlank(form.getParkId())) {
				// 车场号
				criteria.andParkIdEqualTo(Integer.valueOf(form.getParkId()));
			}
			if (!StringUtils.isBlank(form.getCarNum())) {
				// 车牌号
				criteria.andCarnumLike(form.getCarNum());
			}
			if(!StringUtils.isBlank(form.getCarNumStatus())){
				criteria.andStatusEqualTo(Integer.valueOf(form.getCarNumStatus()));
			}
			if(!StringUtils.isBlank(form.getExpiryStatus())){
				criteria.andExpiryStatusEqualTo(Integer.valueOf(form.getExpiryStatus()));
			}
			if(!StringUtils.isBlank(form.getTel())){
//				criteria.andTelEqualTo(form.getTel());
				criteria.andTelLike(form.getTel());
			}
			if(!StringUtils.isBlank(form.getMonthFee())){
				criteria.andMonthFee(form.getMonthFee());
			}
			if(!StringUtils.isBlank(form.getCardType())){
				criteria.andCardTypeEqualTo(Integer.valueOf(form.getCardType()));
			}
			if(!power.isAdminIs()){
				criteria.andParkIdIn(power.getParkList());
			}
			
			criteria.andStatusNotEqualTo(5);//删除的车辆不显示
			ParkDataResult dataResult = carStatisFacade.carlist(example, jumpPageInt, pageLengthInt);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
	}

}
